﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BAL;
using Model;
using System.Web.Services;
using System.IO;

namespace blog.Posts
{
    public partial class SearchResult : System.Web.UI.Page
    //public partial class SearchResult : blog.Class.PageProxy
    {
        public  List<Model.Post> resultpost = new List<Model.Post>();
        protected void Page_Load(object sender, EventArgs e)
        {
            Ajax.Utility.RegisterTypeForAjax(typeof(blog.Posts.SearchResult));
            //string quertstr = (Request.QueryString["querystr"].ToString() == "") ? "": Request.QueryString["querystr"].ToString();
            string quertstr = "postname";
            resultpost = BAL.DoPost.searchresult(quertstr);

        }

        public static string RenderUserControl(string postid, string action)
        {
            if (action == "getlist")
            {
                Page page = new Page();
                UserControl.PostList postlist = (UserControl.PostList)page.LoadControl("/UserControl/PostList.ascx");
                postlist.setid(Convert.ToInt32(postid));
                page.Controls.Add(postlist);
                StringWriter writer = new StringWriter();
                HttpContext.Current.Server.Execute(page, writer, false);
                return writer.ToString();
            }
            else if (action == "getedit")
            {
                Page page = new Page();
                UserControl.EditPost editpost = (UserControl.EditPost)page.LoadControl("/UserControl/EditPost.ascx");
                editpost.setid(Convert.ToInt32(postid));
                page.Controls.Add(editpost);
                StringWriter writer = new StringWriter();
                HttpContext.Current.Server.Execute(page, writer, false);
                return writer.ToString();
            }
            else
            {
                return "";
            }
        }

        [Ajax.AjaxMethod()]
        public int getresultcount()
        {
            return resultpost.Count;
        }
        
        [Ajax.AjaxMethod()]
        public string[] getpostidlist()
        {
            int resultcount=resultpost.Count;
            string[] resultid = new string[resultcount + 1];
            for (int i=0;i<resultcount;i++)
            {
                resultid[i] = resultpost[i].postid;
            }
            return resultid;
        }

    }
}